#include <iostream>
using namespace std;

// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if(head == nullptr)
            return true;
        ListNode* half = halfNode(head);
        ListNode *end = reverse(half);
        half->next = nullptr;
        while(head != nullptr)
        {
            if(head->val != end->val)
                return false;
            head = head->next;
            end = end->next;
        }
        return true;
    }
    ListNode* halfNode(ListNode* head)
    {
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast && fast->next)
        {
            fast = fast->next->next;
            if(fast)
                slow = slow->next;
        }
        return slow;
    }
    ListNode* reverse(ListNode* dummy)
    {
        ListNode* pre = dummy;
        ListNode* cur = dummy->next;
        while(cur)
        {
            ListNode *temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};

int main()
{
    return 0;
}
